//
//  DocMainWindow.m
//  Personal Passlock
//
//  Created by 杨岑 on 5/12/15.
//  Copyright (c) 2015 杨岑. All rights reserved.
//

#import "DocumentWC.h"
#import "Helpers.h"
#import "CategoryDataItem.h"


@interface DocumentWC ()

@end

@implementation DocumentWC
{
    CGFloat _categoryPaneWidth;
}

- (void)awakeFromNib
{
    _categoryPaneWidth = self.categoryPane.bounds.size.width;
    
    //self.documentVC.view.frame = self.documentPane.bounds;
    [self.documentPane addSubview:self.documentVC.view];
    self.documentVC.view.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSDictionary *viewsDictionary = @{/*@"pane": self.documentPane,*/ @"document": self.documentVC.view};
    NSMutableArray* constraints = [NSMutableArray array];
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[document]-0-|" options:0 metrics:nil views:viewsDictionary]];

    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[document]-0-|" options:0 metrics:nil views:viewsDictionary]];

    [self.documentPane addConstraints:constraints];
    //[self.window visualizeConstraints:constraints];
    
    //self.actionsView.bgColor = self.itemsTableView.backgroundColor;
    
    [self loadCategoryList];
}

- (void)loadCategoryList
{
    self.categoryDataSource.delegate = self;
    NSArray* names = [@"all,favorite,:catgrp,login,ccard,snote" componentsSeparatedByString:@","];
    for (NSString* name in names)
    {
        CategoryDataItem* item = [CategoryDataItem create:name];
        item.title = L10N(name);
        item.image = [NSImage imageNamed:NSImageNameActionTemplate];
        item.isGroup = [name characterAtIndex:0] == ':';
        
        [self.categoryDataSource.itemList addObject:item];
    }
    [self.categoryTableView reloadData];
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
    NSLog(@"selection changed");
    NSInteger selectedIndex = self.categoryTableView.selectedRow;
    if (selectedIndex >= 0)
    {
        CategoryDataItem* item = self.categoryDataSource.itemList[selectedIndex];
        [self.documentVC setup:nil];
    }
}

- (IBAction)addItem_click:(id)sender {
}

- (IBAction)deleteItem_click:(id)sender {
}

/*
- (CGFloat)splitView:(NSSplitView *)splitView constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)dividerIndex
{
    if (dividerIndex == 0)
    {
        return fmax(proposedPosition, _categoryPaneWidth);
    }
    return proposedPosition;
}*/

@end
